package com.xzz.greedy;

import java.time.LocalDate;
import java.util.*;

/**
 * @author: hhz
 * @create: 2022-01-13 09:23
 * 判断多个日期范围是否有重合
 **/
public class DatesOverlap {
    public static void main(String[] args) {

        List<LocalDate> d1 = Arrays.asList(LocalDate.parse("2021-01-26"), LocalDate.parse("2021-01-27"));
        List<LocalDate> d2 = Arrays.asList(LocalDate.parse("2021-01-02"), LocalDate.parse("2021-01-09"));
        List<LocalDate> d3 = Arrays.asList(LocalDate.parse("2021-01-11"), LocalDate.parse("2021-01-22"));
        List<LocalDate> d4 = Arrays.asList(LocalDate.parse("2021-01-28"), LocalDate.parse("2021-01-30"));
        List<List<LocalDate>> list = Arrays.asList(d1,d2,d3,d4);
        System.out.println(datesOverlap(list));

        List<LocalDate> d5 = Arrays.asList(LocalDate.parse("2022-01-26"), LocalDate.parse("2022-01-27"));
        List<LocalDate> d6 = Arrays.asList(LocalDate.parse("2022-01-02"), LocalDate.parse("2022-01-09"));
        List<LocalDate> d7 = Arrays.asList(LocalDate.parse("2022-01-11"), LocalDate.parse("2022-01-22"));
        List<LocalDate> d8 = Arrays.asList(LocalDate.parse("2022-01-25"), LocalDate.parse("2022-01-30"));
        List<List<LocalDate>> list2 = Arrays.asList(d5,d6,d7,d8);
        System.out.println(datesOverlap(list2));

    }

    public static boolean datesOverlap(List<List<LocalDate>> list) {
        //按结束时间升序排序
        list.sort(Comparator.comparing(a -> a.get(1)));
        LocalDate end = list.get(0).get(1);
        for (int i = 1; i < list.size(); i++) {
            List<LocalDate> localDates = list.get(i);
            LocalDate start = localDates.get(0);
            if(start.compareTo(end)<0){
                return true;
            }else {
                end = localDates.get(1);
            }
        }
        return false;
    }
}
